package com.wenhua.mapper;

import com.wenhua.pojo.Dormitory;
import com.wenhua.pojo.DormitoryBuilding;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface DormitoryMapper {


    /* 查询宿舍楼 */
    List<DormitoryBuilding> selectDormitoryBuilding(Integer offset, Integer pageSize, String selectString, String sort);

    /* 查询宿舍楼数量 */
    @Select("select count(*) " +
            "from dormitory_building " +
            "where building_name LIKE CONCAT('%', #{selectString}, '%') " +
            "or location LIKE CONCAT('%', #{selectString}, '%') ")
    Integer selectDormitoryBuildingCount(String selectString);

    /* 查询宿舍 */
    List<Dormitory> selectDormitory(Integer offset, Integer pageSize, String selectString, String sort);

    /* 查询宿舍数量 */
    @Select("select count(*) " +
            "from view_dormitory_occupancy " +
            "where dormitory_name LIKE CONCAT('%', #{selectString}, '%') ")
    Integer selectDormitoryCount(String selectString);

    /* 根据宿舍名查询宿舍id,如果没有怎么办? */
    Integer selectDormitoryId(String dormitoryName);

    /* 添加学生宿舍 */
    void addStudentDormitory(Integer studentId, Integer dormitoryId);

    /* 查询所有空余宿舍楼(从视图里面查询包含实际宿舍数量字段) */
    @Select("select * " +
            "from view_dormitory_building_with_count " +
            "where actual_dormitory_count < dormitory_count")
    List<DormitoryBuilding> selectDormitoryBuildingAll();

    /* 添加宿舍 */
    Integer insertDormitory(Dormitory dormitory);

    /* 添加宿舍到宿舍关系表 */
    Integer insertDormitory2DormitoryBuilding(Dormitory dormitory);

    /* 添加宿舍楼 */
    Integer insertDormitoryBuilding(DormitoryBuilding dormitoryBuilding);

    /* 根据宿舍楼id查询可以用的宿舍(实际人数小于最大人数) */
    @Select("select * " +
            "from " +
            "view_dormitory_occupancy " +
            "where building_id = #{buildingId} and " +
            "current_occupancy > actual_occupancy")
    List<Dormitory> selectDormitoryByBuildingId(Integer buildingId);

    /* 更新学生宿舍住宿信息 */
    void updateStudentDormitory(Integer studentId, Integer dormitoryId);

    /* 更新宿舍信息 */
    Integer updateDormitory(Dormitory dormitory);

    /* 更新宿舍和宿舍楼信息 */
    Integer updateDormitory2DormitoryBuilding(Integer buildingId, Integer dormitoryId);

    /* 更新宿舍楼信息 */
    @Update("update dormitory_building " +
            "set " +
            "building_name = #{buildingName}, " +
            "dormitory_count = #{dormitoryCount}, " +
            "location = #{location}, " +
            "floor_count = #{floorCount} "+
            "where building_id = #{buildingId}")
    Integer updateDormitoryBuilding(DormitoryBuilding dormitoryBuilding);

    /* 删除宿舍 */
    @Delete("delete from dormitory_info " +
            "where dormitory_id = #{dormitoryId}")
    Integer deleteDormitory(Dormitory dormitory);

    /* 删除宿舍楼 */
    @Delete("delete from dormitory_building " +
            "where building_id = #{buildingId}")
    Integer deleteDormitoryBuilding(DormitoryBuilding dormitoryBuilding);
}
